package com.symbio.ishenapi.background.controller;

import com.symbio.ishenapi.background.model.TmDrug;
import com.symbio.ishenapi.background.model.TmInformed;
import com.symbio.ishenapi.background.model.TmInformedDrug;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.util.Date;
import java.util.Map;

/**
 * 知情同意书
 */
@RequestMapping("/TmInformed")
public class TmInformedController extends BaseController {

    /**
     * wy:添加知情同意书
     */
    public void saveInformed(){
        TmInformed tmid = (TmInformed) getReqBodyObject(TmInformed.class);
        if (!tmid.save()) {
            renderJsonError(500002);
            return;
        }
        if (addDrugAndInfo(tmid)){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002);
        }
    }

    /**
     * 知情同意书与药品关联
     *     提取方法
     */
    public boolean addDrugAndInfo(TmInformed tmid){
        boolean flag = false;
        String[] split = tmid.getDrugId().replace("\"", "").replace("\"", "").split(",");
        if (tmid.getDrugId() == null && "".equals(tmid.getDrugId()) && tmid.getId() == null && "".equals(tmid.getId())) {
            return flag;
        }
        for (int i = 0; i < split.length; i++) {
            TmInformedDrug t = new TmInformedDrug();
            t.setInformedId(tmid.getId());
            t.setDrugId(new BigInteger(split[i]));
            t.setCreateTime(new Date());
            if (t.findDrugIdAndInfoId(t)) {
                continue;
            }
            t.save();
        }
        flag = true;
        return flag;
    }
    /**
     * wy:查询知情同意书
     */
    public void findPageAll(){
        Map map = TmInformed.dao.findPageAll(getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * 知情同意书与药品关联
     */
    public void infoAddDrug(){
        TmInformed tmid = (TmInformed) getReqBodyObject(TmInformed.class);
        if (addDrugAndInfo(tmid)){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002);
        }
    }
    /**
     * wy;修改知情同意书
     */
    public void updateInformed(){
        TmInformed tmid = (TmInformed) getReqBodyObject(TmInformed.class);
        if (!tmid.update()) {
            renderJsonError(500002);
            return;
        }
        if (addDrugAndInfo(tmid)){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002);
        }
    }

    /**
     * wy;禁用知情同意书
     */
    public void delOneInformed(){
        TmInformed t = (TmInformed)getReqBodyObject(TmInformed.class);
        if (t.update()){
            renderJsonRet(t);
        }else {
            renderJsonError(500002);
        }
    }

    /**
     * wy;删除知情同意书下关联药品
     */
    public void delInfoAndDrug(){
        TmInformedDrug t = (TmInformedDrug)getReqBodyObject(TmInformedDrug.class);
        if (t.delInfoAndDrug(t)){
            renderJsonRet(500001);
        }else {
            renderJsonError(500002);
        }
    }

    /**
     * wy:批量禁用知情同意书
     */
    public void delInformed(){
        String ids = getReqBodyString().replace("\"","").replace("\"","");
        TmInformed tid = new TmInformed();
        if (tid.delInformed(ids)) {
            renderJsonRet(500001);
        }else {
            renderJsonError(500002,"禁用");
        }
    }

    /**
     * 查询一个知情同意书
     */
    public void findOneInfo(){
        TmInformed ti = (TmInformed) getReqBodyObject(TmInformed.class);
        if (ti.getId() != null && !"".equals(ti.getId())){
            renderJsonRet(ti.findOneInfo(ti));
        }else {
            renderJsonError(500002);
        }
    }

    /**
     * wy:根据知情同意书Id查询药品
     */
    public void findInfoByDrug(){
        TmInformedDrug tid = new TmInformedDrug();
        String id = getPara("id");
        tid.setInformedId(new BigInteger(id));
        if (tid.getInformedId() != null && !"".equals(tid.getInformedId())){
            Map infoByDrug = tid.findInfoByDrug(getQueryModel(),tid);
            if (null != infoByDrug && infoByDrug.size() > 0){
                renderPageJsonRet(infoByDrug.get("count"),infoByDrug.get("data"));
            }else {
                renderJsonError(500002,"请重试!");
                return;
            }
        }else {
            renderJsonError(500002);
            return;
        }
    }

    /**
     * 药品列表分页展示
     *  去掉已经有的药品
     */
    public void findPageDrug(){
        TmDrug tmDrug = new TmDrug();
        String ids = getPara("ids");
        Map map = tmDrug.dao().findPageDrug(getQueryModel(),ids);
        if (null != map && map.size() > 0){
            renderPageJsonRet(map.get("count"),map.get("data"));
        }else {
            renderJsonError(40003,"请重试!");
        }
    }

    /**
     * 药品列表分页展示
     *  根据Ids查询药品
     */
    public void findIdsDrug(){
        TmDrug tmDrug = new TmDrug();
        String ids = getPara("ids");
        Map map = tmDrug.findIdsDrug(getQueryModel(),ids);
        if (null != map && map.size() > 0){
            renderPageJsonRet(map.get("count"),map.get("data"));
        }else {
            renderJsonError(40003,"请重试!");
        }
    }
}
